package com.pf.controller;

import com.google.common.collect.ImmutableMap;
import com.pf.dto.UserDTO;
import com.pf.helper.HttpProxyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2021/9/7
 */
@RestController
public class IndexController {

    @Autowired
    private HttpProxyClient httpProxyClient;

    /**
     * 测试GET请求
     * <p>
     * 无请求头
     *
     * @return
     */
    @GetMapping("/test/get")
    public String testGet() {
        String url = "http://localhost:8081/get";
        Map<String, String> reqParams = ImmutableMap.of("requestId", "111", "accessToken", "222", "userId", "1001");
        String result = httpProxyClient.executeGet(url, reqParams, String.class);
        return result;
    }

    /**
     * 测试GET请求
     * <p>
     * 无请求头
     *
     * @return
     */
    @GetMapping("/test/get2")
    public String testGet2() {
        String url = "http://localhost:8081/get2";
        Map<String, String> reqParams = ImmutableMap.of("requestId", "111", "accessToken", "222", "userId", "1002", "userName", "李四", "userPwd", "111111");
        String result = httpProxyClient.executeGet(url, reqParams, String.class);
        return result;
    }


    /**
     * 测试POST请求
     * <p>
     * 请求头：Content-type = application/json
     *
     * @return
     */
    @GetMapping("/test/post")
    public String testPost() {
        String url = "http://localhost:8081/post";
        Map<String, String> reqParams = ImmutableMap.of("requestId", "111", "accessToken", "222");
        UserDTO userDTO = UserDTO.builder().userId(1001).userName("张三").userPwd("123456").build();
        Map<String, String> reqHeaders = ImmutableMap.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        String result = httpProxyClient.executePostJson(url, reqParams, userDTO, reqHeaders, String.class);
        return result;
    }

    /**
     * 测试POST请求
     * <p>
     * 请求头：content-type = application/x-www-form-urlencoded
     *
     * @return
     */
    @GetMapping("/test/post2")
    public String testPost2() {
        String url = "http://localhost:8081/post2";
        Map<String, String> reqParams = ImmutableMap.of("requestId", "111", "accessToken", "222");
        Map<String, Object> reqBodyParams = ImmutableMap.of("userId", "1002", "userName", "李四", "userPwd", "111111");
        Map<String, String> reqHeaders = ImmutableMap.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        String result = httpProxyClient.executePost(url, reqParams, reqBodyParams, reqHeaders, String.class);
        return result;
    }


}
